<?php namespace Admin\Controller; 

use Common\Model\GoodsAttr;
use Common\Model\GoodsList;

//后台控制器
class GoodsListController extends CommonController{
	//自定义构造函数
	public function __auto(){
		
	}


   //商品的货品列表
   public function index(){
   		//第三步.判断是否有货品，如果没有就去添加
   		$gid = Q('get.gid',0,'intval');//获取商品对应的商品id
		$goodsListModel = new \Common\Model\GoodsList;
		$goodsListData = $goodsListModel->get();
		if(!$goodsListData) View::error('先去添加货品',U('add',array('gid'=>$gid)));
	
		//第一步.处理表格表头的规格的数据例如：“颜色，尺码”
		$goodsAttrModel = new GoodsAttr();
		$data = $goodsAttrModel
				->join('type_attr as ta',C('database.prefix') .'goods_attr.wsshop_type_attr_taid','=','ta.taid')
				->where("wsshop_goods_gid={$gid} AND class= '规格' ")
				->groupBy("ta.taid")
				->get();
		//从商品属性表获得添加货品下拉菜单所选择的内容
		foreach ($data as $k => $v) {
			$data[$k]['value'] = $goodsAttrModel->where("wsshop_type_attr_taid={$v['wsshop_type_attr_taid']} AND wsshop_goods_gid={$gid}")->get();
		}
		View::with('data',$data);
	
	
		//第二步.显示以后货品
	    $listData = $goodsListModel->where("wsshop_goods_gid={$gid}")->get();
		
	    foreach ($listData as $k => $v) {
	    	//combineID里面的是商品属性id
	        $listData[$k]['value'] = $goodsAttrModel->where("gaid IN({$v['combineID']})")->groupBy("wsshop_type_attr_taid")->get();
	    }
	    View::with('listData',$listData);
		
		
		//载入货品列表界面
		View::make();
   }


	//添加货品
	public function add(){
		$goodsListModel = new GoodsList();
		//添加动作
		if(IS_POST){
			//如果有值传过来就跳转对应类中的store方法，如果成功
			if($goodsListModel->store()) View::success('添加成功');
			//失败
			View::error($goodsListModel->getError());
		}
		
		
		//添加货品的时候下拉菜单所选择的内容（从商品属性表获得）
		$gid = Q('get.gid',0,'intval');
		//处理表格表头的规格的数据例如：“颜色，尺码”
		$goodsAttrModel = new GoodsAttr();
		$data = $goodsAttrModel
				->join('type_attr as ta',C('database.prefix') .'goods_attr.wsshop_type_attr_taid','=','ta.taid')
				->where("wsshop_goods_gid={$gid} AND class= '规格' ")
				->groupBy("ta.taid")
				->get();
				
		//从商品属性表获得添加货品下拉菜单所选择的内容
		foreach ($data as $k => $v) {
			$data[$k]['value'] = $goodsAttrModel->where("wsshop_type_attr_taid={$v['wsshop_type_attr_taid']} AND wsshop_goods_gid={$gid}")->get();
		}
		View::with('data',$data);
		
		//添加页面
		View::make();
	}
	
	
	//编辑商品货表
	public function edit(){
		$goodsListModel = new \Common\Model\GoodsList;
		//修改货品
		if(IS_POST){
			//如果有post传值就执行模型类中edit方法，如果成功就提示成
			if($goodsListModel->edit()) View::success('修改成功');
			//失败提示失败错误
			View::error($goodsListModel->getError());
		}
			
		
			
		//显示商品货表旧数据
		$glid = Q('get.glid',0,'intval');//要编辑的货品glid
		$oldData = $goodsListModel->where("glid={$glid}")->find();
		$oldData['combineID']= explode(',', $oldData['combineID']);
		View::with('oldData',$oldData);
		
		
		//显示商品规格
		$gid = $oldData['wsshop_goods_gid'];
		//处理表格表头的规格的数据例如：“颜色，尺码”
		$goodsAttrModel = new GoodsAttr();
		$data = $goodsAttrModel
				->join('type_attr as ta',C('database.prefix') .'goods_attr.wsshop_type_attr_taid','=','ta.taid')
				->where("wsshop_goods_gid={$gid} AND class= '规格' ")
				->groupBy("ta.taid")
				->get();
				
		//从商品属性表获得添加货品下拉菜单所选择的内容
		foreach ($data as $k => $v) {
			$data[$k]['value'] = $goodsAttrModel->where("wsshop_type_attr_taid={$v['wsshop_type_attr_taid']} AND wsshop_goods_gid={$gid}")->get();
		}
		View::with('data',$data);
		
		View::make();
	}
   
   
   //删除货品列表
   public function del(){
	   	//获取要删除的glid
	   	$glid=Q('get.glid',0,'intval');
		//删除
		$goodsListModel = new GoodsList();
		$goodsListModel->where("glid={$glid}")->delete();
		View::success('删除成功');
   }
   
}
